<?php

declare (strict_types=1);

namespace App\Model;

use App\Utils\Upload\View;
use Carbon\Carbon;
use Hyperf\DbConnection\Model\Model;

/**
 * Class TaskExec
 * @package App\Model
 * @property mixed|int id
 * @property mixed|int user_id
 * @property mixed|int cooperative_id
 * @property mixed|int employer_id
 * @property mixed|int task_id
 * @property mixed|string getter_time
 * @property mixed|string remaining_time
 * @property mixed|int audit_status
 * @property mixed|int finish_status
 * @property mixed|int arbitration_status
 * @property mixed|int status
 * @property mixed|string gather_user_info  收集用户信息
 * @property mixed|string audit_fail_context    审核失败描述
 * @property mixed|int audit_fail_number
 * @property mixed|int audit_fail_time
 * @property Carbon created_at
 * @property mixed|string updated_at
 * @property mixed|string deleted_at
 * @property mixed|string audit_content
 * @property mixed|string audit_time_end
 *
 * @property mixed|Task task
 * @property TaskArbitration taskArbitration
 * @property User user
 * @property UserEmployer employer
 */
class TaskExec extends Model
{
    protected $table = 'task_exec';


    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function employer()
    {
        return $this->belongsTo(UserEmployer::class, 'employer_id', 'id');
    }

    public function task()
    {
        return $this->belongsTo(Task::class, 'task_id', 'id');
    }

    public function taskArbitration()
    {
        return $this->hasOne(TaskArbitration::class, 'task_exec_id', 'id');
    }

    public function setAuditContentAttribute($value)
    {
        $this->attributes['audit_content'] = implode('|', $value);
    }

    public function getAuditContentAttribute($value)
    {
        if ($value) {
            $images = explode('|', $value);
            $imagesRender = [];
            $viewClass = di(View::class);
            foreach ($images as $v) {
                if ($v) {
                    $imagesRender[] = [
                        'image_id' => $v,
                        'image_path' => $viewClass->fullPath($v)
                    ];
                }
            }
            return $imagesRender;
        } else {
            return $value;
        }
    }

}